package com.example.practice.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.practice.R;
import com.example.practice.adapter.ExercisesDetailAdapter;
import com.example.practice.bean.ExercisesDetailBean;
import com.example.practice.utils.XMLUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExerciseDetailActivity extends AppCompatActivity {
    private TextView tv_title;//标题栏标题控件变量
    private TextView tv_back;//标题栏返回控件变量
    private RelativeLayout rl_bar;//标题栏控件变量
    private ListView lv_list;
    private String title;
    private int id;
    private List<ExercisesDetailBean> ebl;
    private ExercisesDetailAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exercise_detail);
        //设置此界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        id = getIntent().getIntExtra("id",0);
//        Log.i("hh",id+"");
        //获取从习题界面传递过来的章节标题
        title = getIntent().getStringExtra("title");
        ebl = new ArrayList<ExercisesDetailBean>();
        initData();
        initView();
    }
    private void initData(){
        try {
            //从xml文件中获取习题数据
            InputStream is = getResources().getAssets().open("chapter"+id+".xml");
            ebl = XMLUtils.getExercisesInfos(is);
//            Log.i("ttttt","333");
        }catch (Exception e){
            e.printStackTrace();
//            Log.i("xxxxx","hh");
        }

    }

    private void initView(){
        tv_title=(TextView) findViewById(R.id.tv_title);//关联习题详情页面控件标题控件tv_title
        tv_back=(TextView) findViewById(R.id.tv_back);//关联习题详情页面标题栏返回控件tv_back
        rl_bar=(RelativeLayout) findViewById(R.id.rl_bar);//关联习题详情页面标题栏控件rl_bar
        lv_list=(ListView) findViewById(R.id.lv_list);
        TextView tv = new TextView(this);
        tv.setTextColor(Color.parseColor("#000000"));
        rl_bar.setBackgroundColor(Color.parseColor("#60b4f9"));
        tv.setTextSize(16.0f);
        tv.setText("一、选择题");
        tv.setPadding(10,15,0,0);
        lv_list.addHeaderView(tv);
        tv_title.setText(title);
        //标题栏返回控件添加点击事件
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ExerciseDetailActivity.this.finish();
            }
        });
        adapter = new ExercisesDetailAdapter(ExerciseDetailActivity.this);
        adapter.setData(ebl);
        lv_list.setAdapter(adapter);
    }
}